package db;

import Info.SystemEnum;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pojo.CarType;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ManageCarTypeDB {
    private List<CarType> carTypeList;
    private Log log;
    

    public ManageCarTypeDB(Log log) {
        carTypeList =  DataBase.getInstance().getCarTypesList();
        this.log = log;
    }

    public ManageCarTypeDB(List<CarType> carTypeList) {
        this.carTypeList = carTypeList;
        log = LogFactory.getLog(getClass());
    }

    //查找--是否存在某种车型  返回-1找不到 返回其他数 能找到
     private int find(CarType find){
         int index = 0;
         if(find!=null){
             for(CarType f : carTypeList){
                 if(f.equals(find)) break;
                 index+=1;
             }
             if(index<carTypeList.size()){
                 log.info(getClass()+"\tfind\t"+ SystemEnum.SUCCESS.toString());
                 return index;
             }
         }
         log.info(SystemEnum.ERROR_MATCH.toString());
         return -1;
    }
    //查找--是否存在符合某种字段的车型
    public List<CarType> find(String field,String content){
        List<CarType> findByField = new ArrayList<>();
        if(content==null) return findByField;
        try {
            switch (field){
                case "id":
                    for(CarType find : carTypeList){
                        if(find.getId().equals(content)) findByField.add(find);
                    }
                    break;
                case "name":
                    for(CarType find : carTypeList){
                        if(find.getName().equals(content)) findByField.add(find);
                    }
                    break;
                default:
                    log.info(SystemEnum.ERROR_INPUT.toString());
            }
        }catch (Exception e){
            log.info(SystemEnum.ERROR_INPUT.toString());
            return null;
        }
        return findByField;
    }


    //增
    public boolean add(CarType carType){
        if(carType!=null){
            if(find(carType)==-1){
                carTypeList.add(carType);
                log.info(getClass()+"\tadd\t"+SystemEnum.SUCCESS.toString());
                return true;
            }else{
                log.info(SystemEnum.ERROR_REPEAT.toString());
                return false;
            }
        }
        log.info(SystemEnum.ERROR_MATCH.toString());
        return false;
    }

    //删除
    public boolean delete(CarType carType){
        if(carType!=null){
            int index = find(carType);
            if(index == -1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                carTypeList.remove(index);
                log.info(getClass()+"\tdelete\t"+SystemEnum.SUCCESS.toString());
                return true;
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    //修改
    public boolean modify(CarType carType,String field,String content){
        if(carType!=null){
            int index = find(carType);
            if(index==-1){
                log.info(SystemEnum.ERROR_MATCH.toString());
                return false;
            }else{
                CarType find = carTypeList.get(index);
                try {
                    switch (field) {
                        case "id":
                            find.setId(content);
                            break;
                        case "name":
                            find.setName(content);
                            break;
                        default:
                            log.info(SystemEnum.ERROR_INPUT.toString());
                    }
                    log.info(getClass()+"\tmodify\t"+SystemEnum.SUCCESS.toString());
                    return true;
                }catch (Exception e){
                    log.info(SystemEnum.ERROR_INPUT.toString());
                    return false;
                }
            }
        }else{
            log.info(SystemEnum.ERROR_MATCH.toString());
            return false;
        }
    }

    public void printAll(){
        for (CarType carType:carTypeList) {
            System.out.println(carType.getId()+"."+carType.getName());
        }
    }
    public int getListSize(){
        return carTypeList.size();
    }

}